import QtQuick 1.1
import com.nokia.meego 1.0


Page {
    id: demotyPage
    orientationLock: PageOrientation.LockPortrait
    BusyIndicatorBG {
        anchors.top: parent.top
        visible: (demotyModel.status!=XmlListModel.Ready)
    }
    Timer {
        interval: 500
        repeat: true
        running: demotyListView.movingHorizontally
        onTriggered: {
            if( demotyListView.atXBeginning && demotyListView.contentX<-50 ) {
                demotyListView.prevPage = true;
            }
            if (demotyListView.atXEnd && demotyListView.contentX+400>demotyListView.contentWidth) {
                demotyListView.nextPage = true;
            }
        }
    }
    Rectangle {
        height: demotyListView.height
        width: 50
        color: "black"
        anchors.left: parent.left
        visible: demotyListView.prevPage
        z:demotyListView.z+1
        Text {
            id: prevMsg
            anchors.centerIn: parent
            text: "Przeciągnij do poprzenidej strony"
            rotation: 270
            font{
                bold: true
                pointSize: 24
            }
            color: "white"
            //verticalAlignment: Text.AlignVCenter
        }
    }
    Rectangle {
        height: demotyListView.height
        width: 50
        color: "black"
        anchors.right: parent.right
        visible: demotyListView.nextPage
        z:demotyListView.z+1
        Text {
            id: nextMsg
            anchors.centerIn: parent
            text: "Przeciągnij do następnej strony"
            rotation: 90
            font{
                bold: true
                pointSize: 24
            }
            color: "white"
        }
    }
    ListView {
        id: demotyListView
        property bool prevPage: false
        property bool nextPage: false
        anchors.fill: parent
        cacheBuffer: width*4
        snapMode: ListView.SnapToItem
        orientation: ListView.Horizontal
        highlightFollowsCurrentItem: true
        delegate: DelegateDemot {
            id:demotElement
            visible: (!demotyListView.nextPage)&&(!demotyListView.prevPage)

        }
        model: ModelDemoty {id:demotyModel }
        onMovingChanged: {
            if(demotyListView.nextPage) {
                demotyListView.nextPage = false;
                demotyModel.getPgNum(demotyModel.dzial,++demotyModel.pgNumber)
            }
            if (demotyListView.prevPage) {
                demotyListView.prevPage=false;
                demotyModel.getPgNum(demotyModel.dzial,--demotyModel.pgNumber)
            }
        }
        BusyIndicatorBG {
            visible: (demotyListView.nextPage)||(demotyListView.prevPage)
            anchors.top: demotyListView.top
        }
    }




    Image {
        id: dap
        source: "img/show-toolbar.png"
        width: 40
        height: 40
        anchors {
            right: parent.right
            bottom: parent.bottom
        }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                if (demotyAppMenu.status===DialogStatus.Closed) {
                    demotyAppMenu.open();
                    dap.visible=false;
                    dtm.visible=false;
                }
                else {
                    demotyAppMenu.close();
                    dap.visible=true;
                    dtm.visible=true;
                }

            }
        }
    }
    Image {
        id:dtm
        source: "img/show-pages.png"
        width: 40
        height: 40
        anchors {
            left: parent.left
            bottom: parent.bottom
        }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                if (demotyTypeMenu.status===DialogStatus.Closed){
                    demotyTypeMenu.open()
                    dap.visible=false;
                    dtm.visible=false;
                }
                else {
                    demotyTypeMenu.close()
                    dap.visible=true;
                    dtm.visible=true;
                }
            }
        }

        MenuDemotApp{id: demotyAppMenu}
        MenuDemotType{id: demotyTypeMenu}
    }
}
